package com.ssh.cp.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.ssh.cp.entity.User;
import com.ssh.cp.iservice.IAdminService;
import com.ssh.cp.iservice.IFileService;
import com.ssh.cp.iservice.IUserService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;

@Controller
public class AdminAction {
	@Autowired
	private IFileService fileService;
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IAdminService adminService;
    
    private JSONObject result;//用作Json结果
    private int classid;
    
	private int draw , recordsTotal, recordsFiltered,start,length;//用于返回给JQueryDatatables 数据参数
    
	private String users;
    
	private ActionContext ctx;
	private String search;//搜索关键字
	private InputStream inputStream; 
	private String classname;
	 
	private int forbid;
	
	private int theuserid;

	private int fileid;

	public String toAdmin() {
		
		
		return "success";
	}
	
	
	/*
	 * 管理员用户使用，列出所有文件
	 * 后台分页
	 * */
public String listFile2() {
		/*
		System.out.println("========start: "+start);
		
		System.out.println("========length: "+length);
		System.out.println("========draw:"+draw);
		System.out.println("========count:"+fileService.getCount());
		System.out.println("========search:  "+getSearch());
		*/
		
		recordsTotal=fileService.getCount();
		List<?> list=null;
				if(search==null||"".equals(search)||"undefined".equals(search))
					{
					list=fileService.listFile(start,length);
					recordsFiltered=recordsTotal;
					}
				else {
					list=fileService.listFileByFileName(search,start,length);
					recordsFiltered=list.size();
					}
		
		
		result=new JSONObject();
		result.put("draw",draw);
		result.put("recordsTotal",recordsTotal);
		result.put("recordsFiltered",recordsFiltered);
		
		JSONArray array =JSONArray.fromObject(list);		
		
		result.put("data",array);
		
		return "success";
	}
	
	public String listUser() {
		
		
		List<?> list=null;
		
		
		list=userService.listUser();
		int i=list.size();
		result=new JSONObject();
			
		User[] u=(User[])list.toArray(new User[i]);
		
		users=JSON.toJSONString(u); 
		JSONArray array =JSONArray.fromObject(users);
		result.put("data",array);
		//System.out.println(users);
		//System.out.println(result);
		return "success";
	}

	public String delClass() throws IOException {
		ctx=ActionContext.getContext();
		
		int userid=Integer.valueOf(ctx.getSession().get("userid").toString());
		adminService.delClass(userid, classid);
		inputStream = new ByteArrayInputStream(("成功删除类").getBytes("UTF-8"));
		
		return "success";
	}

	
	public String addClass() throws IOException {
		
		adminService.addFileClass(classname);
		System.out.println(classname);
		inputStream = new ByteArrayInputStream(("成功添加类").getBytes("UTF-8"));
		return "success";
	}
	
	public String renameClass() throws IOException {
		
		adminService.renameClass(classid, classname);

		inputStream = new ByteArrayInputStream(("成功修改类").getBytes("UTF-8"));
		return "success";
	}

	
	public String forbidUser() throws Exception {
		ctx=ActionContext.getContext();
		int adminid=Integer.valueOf(ctx.getSession().get("userid").toString());
		if(forbid==1)
			forbid=0;
		else if(forbid==0)
			forbid=1;
		System.out.println(forbid);
		if(adminService.forbidUser(theuserid, adminid, forbid)) {
			inputStream = new ByteArrayInputStream(("成功修改用户").getBytes("UTF-8"));
			return "success";
		}
		else {
			inputStream = new ByteArrayInputStream(("修改用户失败").getBytes("UTF-8"));
		
			return "fail";
		}
	}
	

	
	/*
	 * 删除文件记录，不删除物理文件
	 */
	public String delFile() throws IOException {
		ctx=ActionContext.getContext();
		int adminid=Integer.valueOf(ctx.getSession().get("userid").toString());
		if(adminService.delFile(adminid, fileid)) {
			inputStream = new ByteArrayInputStream(("成功删除文件").getBytes("UTF-8"));
			return "success";
		}
		else {
			inputStream = new ByteArrayInputStream(("删除文件失败").getBytes("UTF-8"));
		
			return "fail";
		}
		
	}
	
	
	
	public String listLoginLog() {
		ctx=ActionContext.getContext();
		int adminid=Integer.valueOf(ctx.getSession().get("userid").toString());
		List<?> list=null;
		list = adminService.listLoginLog(adminid, start, length);
		recordsTotal=adminService.getLoginLogNum();
		recordsFiltered=recordsTotal;
		result=new JSONObject();
		result.put("draw",draw);
		result.put("recordsTotal",recordsTotal);
		result.put("recordsFiltered",recordsFiltered);
		
		JSONArray array =JSONArray.fromObject(list);		
		
		result.put("data",array);
		System.out.println(result.toString());
		return "success";
	}
	
	
	
	
	
	
	
	
	public int getDraw() {
		return draw;
	}


	public void setDraw(int draw) {
		this.draw = draw;
	}



	public JSONObject getResult() {
		return result;
	}


	public void setResult(JSONObject result) {
		this.result = result;
	}


	public int getRecordsTotal() {
		return recordsTotal;
	}


	public void setRecordsTotal(int recordsTotal) {
		this.recordsTotal = recordsTotal;
	}


	public int getRecordsFiltered() {
		return recordsFiltered;
	}


	public void setRecordsFiltered(int recordsFiltered) {
		this.recordsFiltered = recordsFiltered;
	}


	public int getStart() {
		return start;
	}


	public void setStart(int start) {
		this.start = start;
	}


	public int getLength() {
		return length;
	}


	public void setLength(int length) {
		this.length = length;
	}


	public String getSearch() {
		return search;
	}


	public void setSearch(String search) {
		this.search = search;
	}


	public int getClassid() {
		return classid;
	}


	public void setClassid(int classid) {
		this.classid = classid;
	}


	public String getUsers() {
		return users;
	}


	public void setUsers(String users) {
		this.users = users;
	}


	public InputStream getInputStream() {
		return inputStream;
	}


	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}


	public String getClassname() {
		return classname;
	}


	public void setClassname(String classname) {
		this.classname = classname;
	}


	public int getForbid() {
		return forbid;
	}


	public void setForbid(int forbid) {
		this.forbid = forbid;
	}

	
	public int getTheuserid() {
		return theuserid;
	}


	public void setTheuserid(int theuserid) {
		this.theuserid = theuserid;
	}


	public int getFileid() {
		return fileid;
	}


	public void setFileid(int fileid) {
		this.fileid = fileid;
	}
}
